package com.example.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.gulimall.product.entity.AttrEntity;
import com.example.gulimall.product.service.AttrAttrgroupRelationService;
import com.example.gulimall.product.service.AttrService;
import com.example.gulimall.product.service.CategoryService;
import com.example.gulimall.product.vo.AttrGroupRelationVo;
import com.example.gulimall.product.vo.AttrGroupWithAttrsRespVo;
import com.example.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.gulimall.product.entity.AttrGroupEntity;
import com.example.gulimall.product.service.AttrGroupService;
import com.example.gulimall.common.utils.PageUtils;
import com.example.gulimall.common.utils.R;



/**
 * 属性分组
 *
 * @author xie
 * @email 2949459108@gmail.com
 * @date 2024-01-18 21:07:08
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable(value = "catelogId") Long cateLogId){
PageUtils page=attrGroupService.queryPage(params,cateLogId);
        return R.ok().put("page", page);
    }

    /**
     * 获取属性分组
     * @param params
     * @param cateLogId
     * @return
     */
    @RequestMapping("/listPro/{catId}")
    public R listPro(@RequestParam Map<String, Object> params,@PathVariable(value = "catId")long cateLogId){
        PageUtils page=attrGroupService.queryPage(params, cateLogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
         attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R update(@RequestBody Long[] brandIds){
      attrGroupService.removeAttrGroupByIds(Arrays.asList(brandIds));

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping ("/attr/relation/delete")
    public R delete(@RequestBody AttrGroupRelationVo[] attrGroupRelationVo){
		attrService.deleteRelation(attrGroupRelationVo);
        return R.ok();
    }


    @GetMapping("/{attrGroupId}/attr/relation")
    public R listAttrRealation(@PathVariable(value = "attrGroupId")long attrGroupId){
        List<AttrEntity> data = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data",data);
    }
    @GetMapping("/{attrGroupId}/noattr/relation")
    public R listNoAttrRealation(@RequestParam Map<String, Object> params,@PathVariable(value = "attrGroupId")long attrGroupId){
        PageUtils page=attrService.getNoAttrRelation(attrGroupId,params);
        return R.ok().put("page",page);
    }

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRelationVo> attrGroupRelationVos){
        attrAttrgroupRelationService.saveRelation(attrGroupRelationVos);
        return R.ok();
    }
    @GetMapping("/{catalogId}/withattr")
    public R getAtrrGroupsWithAttr(@PathVariable(value = "catalogId") Long catalogId){
        List<AttrGroupWithAttrsRespVo>attrGroupWithAttrsRespVoList=attrGroupService.getAttrGroupsWithAttrs(catalogId);
        return R.ok().put("data",attrGroupWithAttrsRespVoList);
    }
}
